package gold.digger;

import gold.utils.InputUtil;

import java.util.Arrays;
import java.util.Comparator;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC1029 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int twoCitySchedCost(int[][] costs) {
            Arrays.sort(costs, new Comparator<int[]>() {
                @Override
                public int compare(int[] o1, int[] o2) {
                    return o1[1] - o1[0] - (o2[1] - o2[0]);
                }
            });
            int ans = 0, counter = 0;
            for (int[] cost : costs) {
                ans += counter++ >= costs.length / 2 ? cost[0] : cost[1];
            }
            return ans;
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.twoCitySchedCost(InputUtil.toDoubleIntegerArray("[[10,20],[30,200],[400,50],[30,20]]")));
        System.out.println(solution.twoCitySchedCost(InputUtil.toDoubleIntegerArray("[[259,770],[448,54],[926,667],[184,139],[840,118],[577,469]]")));
        System.out.println(solution.twoCitySchedCost(InputUtil.toDoubleIntegerArray("[[515,563],[451,713],[537,709],[343,819],[855,779],[457,60],[650,359],[631,42]]")));
    }

    public static void main(String[] args) throws Exception {
        LC1029 an = new LC1029();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
